<?php

use Phalcon\Mvc\Model;
use Phalcon\Exception;

class UsersModel extends Model {
    /**
     * @var int user id. primary key, auto incremental
     *
     * @Id
     * @Column(type="integer")
     */
    private $id;

    /**
     * @var string user full name
     *
     * @Column(type="string")
     */
    private $name;

    /**
     * @var int user gender. 1 stands for male, 0 stands for female. default is 0, allow null
     *
     * @Column(type="int")
     */
    private $gender;

    /**
     * @var string user job.
     *
     * @Column(type="string")
     */
    private $job;


	/**
	 * @todo: we need to have a database meta table to deal with this.
	 *      It's nice to have something like $model->getJqxDataField('userTableName') then return meta data from DB.
	 *
	 * Samples:
	 * { name: 'name', type: 'string' },
	 * { name: 'type', type: 'string' },
	 * { name: 'calories', type: 'int' },
	 * { name: 'totalfat', type: 'string' },
	 * { name: 'protein', type: 'string' }
	 *
	 * @return array
	 */
    public function getJqxDataField() {
        return array(
	        array('name' => 'id',       'type' => 'int'),
	        array('name' => 'name',     'type' => 'string'),
	        array('name' => 'gender',   'type' => 'int'),
	        array('name' => 'job',      'type' => 'string')
        );
    }

	/**
	 * @todo: we need to have a database meta table to deal with this.
	 *      It's nice to have something like $model->getJqxColumns('userTableName') then return meta data from DB.
	 *
	 * Samples:
	 * { text: Label, datafield: columnName, width: 250 },
	 * { text: 'Beverage Type', datafield: 'type', width: 250 },
	 * { text: 'Calories', datafield: 'calories', width: 180 },
	 * { text: 'Total Fat', datafield: 'totalfat', width: 120 },
	 * { text: 'Protein', datafield: 'protein', minwidth: 120 }
	 *
	 * @return array
	 */
	public function getJqxColumns() {
		return array(
			array('text' => 'User ID',      'datafield' => 'id'),
			array('text' => 'Full Name',    'datafield' => 'name'),
			array('text' => 'Gender',       'datafield' => 'gender'),
			array('text' => 'Job Title',    'datafield' => 'job'),
		);
	}

    public function columnMap()
    {
        // Keys are the real names in the table and
        // the values their names in the application
        return array(
            'ID'        => 'id',
            'name'      => 'name',
            'gender'    => 'gender',
            'job'       => 'job'
        );
    }

    public function __set($key, $value) {
        if (is_string($key) && property_exists(__CLASS__, $key)) {
            return $this->$key = $value;
        } else {
            throw New Exception('Could not get property: ' . $key . ' in class: ' . __CLASS__);
        }
    }

    public function __get($key) {
        if (is_string($key) && property_exists(__CLASS__, $key)) {
            return $this->$key;
        } else {
            throw New Exception('Could not get property: ' . $key . ' in class: ' . __CLASS__);
        }
    }

    public function initialize() {
        $this->setSource("user");
    }

    public function getSource()
    {
        return "user";
    }
}
